#include "UART/WIFI.h"
#include <stdio.h>
#include <string.h>
#include "Delay.h"

/**
 ****************************************************************************************************
 * DONE 用户变量区
 ****************************************************************************************************
 */

WIFI wifi;

uint8_t CONNECT_FLEG;

GPS  gps;
/**
 ****************************************************************************************************
 * 初始化
 ****************************************************************************************************
 */

void WIFI_BUFF_MEMSET(void)
{
	memset(wifi.BUFF,0,BUFF_MAX);
	wifi.buff_flag = 0;
	wifi.buff_length = 0;
}

void WIFI_SEND(UART_HandleTypeDef  *huart,uint8_t *data)
{
//	HAL_UART_Transmit(data,strlen((char *)data));
	USART_printf(huart,"%s",data);
}
/**
 * 功能：检测WIFI的应答
 * 参数：
 *		 cmd：WIFI接收到数据的缓存区
 *       dest：要检测的应答信号
 *       timeout: 等待应答信号的查询超时时间
 * 返回值：
        1 代表指令成功
		0 代表响应超时
 *				 						
 */
uint8_t WIFI_CheckAck(char* cmd, char* dest, int timeout)
{
	char *check = NULL;
	//清空缓冲
	 WIFI_BUFF_MEMSET();
	
	 WIFI_SEND(G4_huart,(uint8_t *)cmd);
	 timeout = timeout/10;
	 while(timeout--)
	 {
		delay_us(1000);//延时1ms
		check = strstr((char *)wifi.BUFF,dest);
		if(check != NULL)
			return 1; 		

	 }
	 WIFI_BUFF_MEMSET();//数据有误 清空	
	 return 0;//超时	
}

void C511_TO_MQTT(void)
{
	WIFI_CheckAck("AT+MDISCONNECT\r\n","OK",1500);
	HAL_Delay(1000);
	WIFI_CheckAck("ATE0\r\n","OK",1500);
	while(!WIFI_CheckAck("AT+QICSGP=1,1,\"\",\"\",\"\"\r\n","OK",5000));
	WIFI_BUFF_MEMSET();//数据有误 清空	
	HAL_Delay(1000);
	if(WIFI_CheckAck("AT+NETOPEN?\r\n","NETOPEN:0",3000))
	{
		while(!WIFI_CheckAck("AT+NETOPEN\r\n","NETOPEN:SUCCESS",7000));
	}
	WIFI_CheckAck("AT+MCONFIG=\"k1zy30ORGH4.esp8266|securemode=2,signmethod=hmacsha256,timestamp=1745488423444|\",esp8266&k1zy30ORGH4,fb68f5e1c06f1bae8b63607392af36137c7490d95c1c51fd2c82a79e76b003c6\r\n","OK",3000);
	
	while(!WIFI_CheckAck("AT+MIPSTART=\"iot-06z00g05wx1h291.mqtt.iothub.aliyuncs.com\",1883,4\r\n","MIPSTART: SUCCESS",3000));
	
	while(!WIFI_CheckAck("AT+MCONNECT=1,60\r\n","MCONNECT: SUCCESS",3000));
	
	while(!WIFI_CheckAck("AT+MSUB=\"/sys/k1zy30ORGH4/esp8266/thing/event/property/post_reply\",0\r\n","MSUB: SUCCESS",4000));
	
	// while(!WIFI_CheckAck("AT+MPUB=\"4G\",0,0,\"hello world\"\r\n","MPUB: SUCCESS",3000));
}
void C511_TO_UDP(uint8_t* IP ,uint8_t *PORT)
{
	uint8_t buf[64];
	WIFI_CheckAck("AT+RESET\r\n","OK",1500);
	while(!WIFI_CheckAck("ATE0\r\n","OK",1500));
	
	if(WIFI_CheckAck("AT+CIPMODE?\r\n","CIPMODE:0",3000))
	{
		WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1500);
	}
	while(!WIFI_CheckAck("AT+QICSGP=1,1,\"cmnbiot\",\"\",\"\"\r\n","OK",5000));

	WIFI_BUFF_MEMSET();//数据有误 清空	
	HAL_Delay(1000);
	if(WIFI_CheckAck("AT+NETOPEN?\r\n","NETOPEN:0",3000))
	{
		while(!WIFI_CheckAck("AT+NETOPEN\r\n","NETOPEN:SUCCESS",7000));
	}
	HAL_Delay(1000);
	sprintf((char *)buf,"AT+CIPOPEN=0,\"UDP\",\"%s\",%s\r\n",IP,PORT);
	while(!WIFI_CheckAck((char *)buf,">",5000));
	
	WIFI_BUFF_MEMSET();
	CONNECT_FLEG = 1;
}
void C511_TO_TCP(uint8_t* IP ,uint8_t *PORT)
{
	uint8_t buf[64];
	WIFI_CheckAck("AT+RESET\r\n","OK",1500);
	while(!WIFI_CheckAck("ATE0\r\n","OK",1500));
	
	if(WIFI_CheckAck("AT+CIPMODE?\r\n","CIPMODE:0",3000))
	{
		WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1500);
	}
	while(!WIFI_CheckAck("AT+QICSGP=1,1,\"cmnbiot\",\"\",\"\"\r\n","OK",5000));

	WIFI_BUFF_MEMSET();//数据有误 清空	
	HAL_Delay(1000);
	if(WIFI_CheckAck("AT+NETOPEN?\r\n","NETOPEN:0",3000))
	{
		while(!WIFI_CheckAck("AT+NETOPEN\r\n","NETOPEN:SUCCESS",7000));
	}
	HAL_Delay(1000);
	sprintf((char *)buf,"AT+CIPOPEN=0,\"TCP\",\"%s\",%s\r\n",IP,PORT);
	while(!WIFI_CheckAck((char *)buf,">",5000));
	
	WIFI_BUFF_MEMSET();
	CONNECT_FLEG = 1;
}
/**
 * 功能：连接至TCP网络进行通信
 * 参数：
 *		 id:WIFI名称
 *       password:WIFI密码
 * 返回值：
        1 代表指令成功
		0 代表响应超时
 *				 						
 */
void CONNECT_TO_TCP(uint8_t* id,uint8_t *password)
{
	uint8_t buf[50];
	WIFI_CheckAck("+++",(char*)id,1500); //确保其退出透传
	while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); 
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));
	while(!WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1000)!=0);
	while(!WIFI_CheckAck("AT+CIPSTART=TCP,192.168.0.146,2347\r\n","+CIPSTART:1",3000));
	while(!WIFI_CheckAck("AT+CIPSEND\r\n","OK",1000));
	WIFI_SEND(G4_huart,":Connected\r\n");
	CONNECT_FLEG = 1;
}

void CONNECT_TO_UDP(uint8_t* id,uint8_t *password)
{
	uint8_t buf[50];

	WIFI_CheckAck("+++",(char*)id,1500); //确保其退出透传
	while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); 
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));
	while(!WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1000)!=0);
	while(!WIFI_CheckAck("AT+CIPSTART=UDP,192.168.0.146,2345,1112,0\r\n","+CIPSTART:1",3000)); //192.168.0.150,2345为IP地址 2345 端口号1112 是模块设置的端口号 0 UDP固定目标模式 
	while(!WIFI_CheckAck("AT+CIPSEND\r\n","OK",1000));
	WIFI_SEND(G4_huart,":Connected\r\n");
	CONNECT_FLEG = 1;
}
void CONNECT_TO_MQTT(uint8_t* id,uint8_t *password)
{
	uint8_t buf[50];
	while(!WIFI_CheckAck("AT+MQTTCLEAN\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); //STA模式
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));//连接WIFI
	while(!WIFI_CheckAck("AT+MQTTLONGCLIENTID=WF-TEST2\r\n","OK",1000)!=0);//配置 MQTT 客户端所需的客户端 ID、用户名和密码
	while(!WIFI_CheckAck("AT+MQTTCONN=broker.emqx.io,1883,0\r\n","MQTTCONNECTED:",2000)!=0);//连接 MQTT 服务器
	while(!WIFI_CheckAck("AT+MQTTSUB=test-app,0\r\n","OK",2000)!=0);//订阅主题 test-app
	while(!WIFI_CheckAck("AT+MQTTPUBRAW=test-w,32,0,0\r\n","OK",2000)!=0);
	WIFI_SEND(G4_huart,"MQTT_Connected\r\n");
	WIFI_BUFF_MEMSET();
	CONNECT_FLEG = 1;
}


